<?php
namespace app\api\controller\pifa;

use app\common\controller\Api;
use think\Config;
use think\Db;
use think\Validate;
use think\Session;
use fast\WechatMini;
use addons\third\model\Third;
use think\Log;
use think\Request;
use fast\Http;
use think\Exception;
use addons\third\library\Service;
use addons\epay\library\Service as PayService;
use app\api\library\Order as PayOrder;
use think\addons\Service as addonsService;
use addons\cms\library\Service as CmsService;
use addons\cms\model\Archives;
use addons\cms\model\Channel;
use addons\cms\model\Diydata;
use addons\cms\model\Fields;
use addons\cms\model\Modelx;
use addons\cms\model\Page;
use addons\cms\model\Special;
use fast\Tree;
use think\Cache;
/**
 * 首页接口
 */
class Cms extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    public function _initialize()
    {
        $this->token = $this->request->post('token');

        parent::_initialize();

    } 
 
    public function getCmsnav()
    {

        $post = $this->request->request();    
        $result['channel'] = Db::table('fa_cms_channel')->cache(true)->find($post['id']);

        $childrenIds = \addons\cms\model\Channel::getChannelChildrenIds($post['id'], false);
        $map['id'] = ['in',$childrenIds];
        $chilrd = Db::table('fa_cms_channel')->cache(true)->where($map)->order('weigh desc')->select();
        $result['list1'] = [];
        $nav = [];
        // $nav[0] = $result['channel'];
        // $nav[0]['name'] = '全部';
        foreach ($chilrd as $key => $value) {
            $nav[$key] = $value;
        }

        $result['list1'] = $nav;


        $this->success("登录成功", $result);

    }


       /**
     * 读取文章列表
     */
    public function arclist()
    {
        $params = [];
        $model = (int)$this->request->request('model');
        $channel = (int)$this->request->request('channel');
        $type = $this->request->request('type');
        $page = (int)$this->request->request('page');
        $pagesize = (int)$this->request->request('pagesize');
        $special = (int)$this->request->request('special');
        $addon = $this->request->request('addon');
        $paginate = $this->request->request('paginate');
        $status = $this->request->request('status');
        $orderstate = $this->request->request('orderstate');
        $keyword = $this->request->request('keyword');
        $pagesize = $pagesize ?: 12;

        if ($model) {
            $params['model'] = $model;
        }
        
        if ($type) {
            $params['type'] = $type;
        }

        if ($page) {
            $params['page'] = $page;
        }
       
        if ($keyword) {
            $params['keyword'] = $keyword;
        }

        if ($pagesize) {
            $params['pagesize'] = $pagesize;
        }


        if ($paginate) {
            $params['paginate'] = true;
        }

        if ($addon) {
            $params['addon'] = $addon;
        }

        if ($status) {
            $params['status'] = $status;
        }


        if ($channel) {
            $channelInfo = Channel::get($channel);
            if (!$channelInfo || $channelInfo['status'] != 'normal') {
                $channel = -1;
            }
            $params['channel'] = $channel;
        }
       
    
       

        $list = Archives::getArchivesList($params);
       

        $this->success('读取成功', $list);

    }

    


     /**
     * 读取文章数据
     */
    public function archives()
    {
        $id = $this->request->request("id/d");
        $archives = Archives::get($id, ['channel']);
        if (!$archives || $archives['status'] != 'normal' || $archives['deletetime']) {
            $this->error("文章未找到");
        }
        $channel = Channel::get($archives['channel_id']);
        if (!$channel) {
            $this->error("栏目未找到");
        }
        $model = Modelx::get($channel['model_id'], [], true);
        if (!$model) {
            $this->error("文章模型未找到");
        }
        $addon = db($model['table'])->where('id', $archives['id'])->find();
        if ($addon) {
            if ($model->fields) {
                CmsService::appendTextAndList('model', $model->id, $addon);
            }
            $archives->setData($addon);
        } else {
            $this->error('文章副表数据未找到');
        }
        $content = $archives->content;
        $archives->views = $archives->views+1;
        $archives->save();
        //移除分页数据
        $content = str_replace("##pagebreak##", "<br>", $content);
        $archives->content = $content;
        CmsService::appendTextAndList('archives', 0, $archives);
        $this->success("读取成功", $archives->toArray());
       
    }

}
